package ca.uwindsor.cs.deepweb.utility.documentanalysis;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Extract terms from specified positions
 * 
 * @author Liang Jie
 * 
 */
public class TermsExtractorfromPosition {

	protected int startposition;

	protected int endposition;

	protected String termsfile;

	public TermsExtractorfromPosition(String file, int start, int end) {
		this.startposition = start;
		this.endposition = end;
		this.termsfile = file;
	}

	public void getTerms() {
		int position = 1;
		int quality = endposition - startposition + 1;
		InputStreamReader reader = null;
		BufferedReader bufferedreader_in;
		String string_line = new String();
		try {
			reader = new InputStreamReader(new FileInputStream(termsfile));
			bufferedreader_in = new BufferedReader(reader);
			string_line = bufferedreader_in.readLine();
			Pattern p = Pattern.compile("^[a-z.@]+$");
			Matcher matcher;
			String temp;
			while (string_line != null) {
				if (position >= startposition && quality != 0) {
					temp = string_line.split("\t")[0];
					// temp = string_line;
					matcher = p.matcher(temp);
					if (matcher.find() && temp.length() > 2 							&& !temp.contains(":") && !temp.contains("-")
							&& !temp.contains("[") && !temp.contains("]")
							&& !temp.contains("!") && !temp.contains(")")
							&& !temp.contains("(") && !temp.contains("\"")
							&& !temp.contains("*")) {
						quality--;
						System.out.println(temp);
					}
				}
				position++;
				string_line = bufferedreader_in.readLine();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		position--;
		System.err.println("There are:" + position + " terms extracted.");
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// args = new String[3];
		// args[0] =
		// "/var/research/estimation/experiment/documentanalysis/gov2_subset0_terms.txt"
		// ;
		// args[1] = "100000";
		// args[2] = "200000";

		if (args.length != 3) {
			System.out
					.println("java TermsExtractorfromPosition termsfile startingposition ending position");
			System.exit(0);
		}
		TermsExtractorfromPosition e = new TermsExtractorfromPosition(args[0],
				Integer.parseInt(args[1]), Integer.parseInt(args[2]));
		e.getTerms();

	}

}
